package com.teamall.web.app.controller;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.Utils;
import com.teamall.web.app.vo.AppNoticeDetailVO;
import com.teamall.web.app.vo.AppNoticeVO;
import com.teamall.web.app.vo.FamilyHelpVO;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.family.db.po.FamilyHelpPO;
import com.teamall.web.sys.db.po.SysFilePO;
import com.teamall.web.sys.db.po.SysInfoPO;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.service.ISysFileService;
import com.teamall.web.sys.service.ISysInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-08-23 22:20
 **/
@RequestMapping("/app/xxgl")
@Controller
public class AppXxglController {
    @Autowired
    private ISysInfoService sysInfoService;
    @Autowired
    private ISysFileService sysFileService;
    @RequestMapping("/getNoticeList")
    @ResponseBody
    public ResResult getNoticeList(Page<SysInfoPO> page) throws BaseAppException{
        return this.getInfoList(page,"1");
    }
    @RequestMapping("/getCaseList")
    @ResponseBody
    public ResResult getCaseList(Page<SysInfoPO> page) throws BaseAppException{
        return this.getInfoList(page,"2");
    }
    @RequestMapping("/getPolicyList")
    @ResponseBody
    public ResResult getPolicyList(Page<SysInfoPO> page) throws BaseAppException{
        return this.getInfoList(page,"0");
    }
    @RequestMapping("/getNoticeDetail")
    @ResponseBody
    public ResResult getNoticeDetail(Integer infoId) throws BaseAppException{
        return this.getInfo(infoId);
    }
    @RequestMapping("/getCaseDetail")
    @ResponseBody
    public ResResult getCaseDetail(Integer infoId) throws BaseAppException{
        return this.getInfo(infoId);
    }
    @RequestMapping("/getPolicyDetail")
    @ResponseBody
    public ResResult getPolicyDetail(Integer infoId) throws BaseAppException{
        return this.getInfo(infoId);
    }
    public ResResult getInfo(Integer infoId) throws BaseAppException{
        ResResult resResult = new ResResult();
        AppNoticeDetailVO noticeVO = new AppNoticeDetailVO();
        SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(infoId);
        BeanUtils.copyProperties(sysInfoPO, noticeVO);
        if(Utils.notEmpty(sysInfoPO.getAtttchUrls())){
            String[] urlArray = sysInfoPO.getAtttchUrls().split(",");
            for (String attachment:urlArray){
                String fileId = attachment.split("=")[1];
                SysFilePO po = new SysFilePO();
                po.setPhotoCode(fileId);
                SysFilePO resultPO = sysFileService.selectByArg(po).get(0);
                Map map = new HashMap<>();
                map.put("file_attachment",attachment);
                map.put("file_name",resultPO.getFileName());
                map.put("file_extension","");
                noticeVO.getAttachList().add(map);
            }
        }
        resResult.setRspData(noticeVO);
        return resResult;
    }
    public ResResult getInfoList(Page<SysInfoPO> page,String type) throws BaseAppException{
        ResResult resResult = new ResResult();
        SysInfoPO sysInfoPO = new SysInfoPO();
        sysInfoPO.setInfoType(type);
        sysInfoService.selectByArgAndPage(sysInfoPO,page);
        Page<AppNoticeVO> voPage = new Page<AppNoticeVO>();
        BeanUtils.copyProperties(page, voPage);
        List<SysInfoPO> resultList = page.getResultList();
        List<AppNoticeVO> voList = new ArrayList<AppNoticeVO>();
        for (SysInfoPO result : resultList) {
            AppNoticeVO vo = new AppNoticeVO();
            BeanUtils.copyProperties(result, vo);
            voList.add(vo);
        }
        voPage.setResultList(voList);
        resResult.setRspData(voPage);
        return resResult;
    }
}
